package com.tjy.wfx.controller;

import com.alibaba.fastjson.JSONArray;
import com.tjy.wfx.model.Result;
import com.tjy.wfx.model.SysModule;
import com.tjy.wfx.model.SysRole;
import com.tjy.wfx.service.SysModuleService;
import com.tjy.wfx.service.SysRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * 角色菜单授权controller
 */
@Controller
@RequestMapping("/role")
public class WfxRoleController {
    @Autowired
    private SysModuleService sysModuleService;
    @Autowired
    private SysRoleService sysRoleService;

    //角色菜单页面
    @RequestMapping("/role-list")
    public String role(Model model) {
        //查询所有的角色信息俩表
        List<SysRole> role = sysRoleService.findRoles();
        model.addAttribute("roleList", role);
        return "role-list";
    }

    //角色赋权的方法
    //路径传参一定要加'/'
    @RequestMapping("/role-add/{roleCode}")
    public String roleAdd(Model model, @PathVariable String roleCode) {
        //查询所有的菜单信息树形菜单信息
//        List<Menu> menus = menuService.selectMenu();
//        model.addAttribute("menus", JSONArray.toJSONString(menus));
        //根据角色id查询角色的菜单信息
        List<SysModule> moduleList =sysModuleService.findRoleMenu(roleCode);
        model.addAttribute("moduleList", JSONArray.toJSONString(moduleList));
        //查询指定id的角色信息
        SysRole sysRole = sysRoleService.findById(roleCode);
        model.addAttribute("sysRole", sysRole);
        return "role-add";
    }

    @RequestMapping("/savePerms")
    @ResponseBody
    public Result save(String roleId, String[] moduleArry){
        System.out.println(roleId+"----"+moduleArry);
        return sysRoleService.savePerms(roleId,moduleArry);
    }
}
